package com.wangjw.netty.designPatten;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.CompositeByteBuf;
import io.netty.buffer.Unpooled;

/**
 * Created by wangjw on 18/7/5.
 */
public class IterableDemo {

    public static void main(String[] args) {
        ByteBuf header = Unpooled.wrappedBuffer(new byte[] {1, 2, 3});
        ByteBuf body = Unpooled.wrappedBuffer(new byte[] {4, 5, 6});

        ByteBuf merge = merge1(header, body);
        merge.forEachByte(value -> {
            System.out.println(value);
            return true;
        });
    }

    public static ByteBuf merge(ByteBuf header, ByteBuf body) {
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.ioBuffer();
        byteBuf.writeBytes(header);
        byteBuf.writeBytes(body);
        return byteBuf;
    }

    // 0拷贝
    public static ByteBuf merge1(ByteBuf header, ByteBuf body) {
        CompositeByteBuf byteBuf = ByteBufAllocator.DEFAULT.compositeBuffer(2);
        byteBuf.addComponent(true, header);
        byteBuf.addComponent(true, body);
        return byteBuf;
    }
}
